//
//  ScoreCard.swift
//  MovieAppDemo
//
//  Created by Demo on 2025/1/26.
//

import SwiftUI

struct ListViewTags: View {
    @State private var searchType: [String] = ["视频", "动画", "漫画"]
    @State var History: [String] = ["全部"]
    @State private var selectedOption: Int = 0
    @State private var hasAppeared = false
    @StateObject private var viewModel = InitViewModel()
    @Binding var tag:String
    @Binding var type:Int
    
    
    
    var body: some View {
        let searchHistory: [String] = viewModel.initdata ?? History
        VStack(spacing: 0) {
            VStack {
                RoundedRectangle(cornerRadius: 10.0)
                    .foregroundStyle(.gray)
                    .frame(width: 50, height: 5)
            }
            .padding()
            .frame(height: 80, alignment: .top)
            .frame(maxWidth: .infinity)
            .background(Color("BgColor1"))
            .clipShape(RoundedRectangle(cornerRadius: 50.0))
            //            .offset(y: 40)
            
            
            VStack {
                Text("请选择分类")
                    .font(.system(size: 30, weight: .bold))
                    .foregroundStyle(.white)
                    .padding(.bottom)
                
                ZStack {
                    RoundedRectangle(cornerRadius: 10)
                        .fill(Color.gray.opacity(0.2))
                        .frame(height: 50)
                    
                    // 滑动背景
                    RoundedRectangle(cornerRadius: 10)
                        .fill(Color("BgColor1"))
                        .frame(width: (UIScreen.main.bounds.width-20) / 3, height: 40)
                        .offset(x: CGFloat(selectedOption - 1) * (UIScreen.main.bounds.width / 3)+CGFloat(selectedOption - 1) * -10, y: 0)
                        .animation(.spring(response: 0.3, dampingFraction: 0.7, blendDuration: 0), value: selectedOption)
                    
                    
                    HStack(spacing: 0) {
                        ForEach(0..<3) { index in
                            Button(action: {
                                selectedOption = index
                                viewModel.get_initdata(type:selectedOption)
                            }) {
                                Text(searchType[index])
                                    .font(.system(size: 16, weight: .medium))
                                    .foregroundColor(selectedOption == index ? .white : .gray)
                                    .frame(width: (UIScreen.main.bounds.width-20) / 3, height: 40, alignment: .center) // 确保文字居中
                                //                                    .background(.red)
                            }
                            
                        }
                    }
                    
                    
                }
                .padding(.horizontal,10)
                
                
                LazyVGrid(columns: [
                    GridItem(.adaptive(minimum: 80), spacing: 10)  // 每个 item 至少占据 80 点宽度，超出时会扩展
                ], spacing: 10) {
                    ForEach(searchHistory, id: \.self) { historyItem in
                        Text(historyItem)
                            .frame(width:80,height:40)
                            .font(.system(size: 18))
                            .foregroundStyle(.white)
                            .padding(.leading,4)
                        
                            .background(Color.white.opacity(0.2))  // 背景色
                            .clipShape(RoundedRectangle(cornerRadius: 6))  // 圆角
                            .overlay(
                                RoundedRectangle(cornerRadius: 6)  // 边框的圆角
                                    .stroke(Color.gray, lineWidth: 1)  // 灰色边框
                            )
                            .fixedSize(horizontal: true, vertical: false)  // 使宽度根据内容自动扩展
                            .padding(.bottom,4)
                            .onTapGesture {
                                // 选择历史项时的动作
                                tag = historyItem
                                
                            }
                    }
                }
                .padding(5)
                .clipShape(RoundedRectangle(cornerRadius: 10))
                .padding(5)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
            
            .offset(y: -40)
        }
        .onAppear {
            if !hasAppeared {
                // 执行首次加载的代码
                selectedOption = type - 1
                if selectedOption < 0 {
                    selectedOption = 0
                }
                viewModel.get_initdata(type:selectedOption)
                hasAppeared = true
                
                
            }
        }
        .background(Color("BgColor1"))
        .ignoresSafeArea(.all)
    }
}




struct ListViewTagser_Previews: PreviewProvider {
    @State static var tag:String = "WEBTOON"
    @State static var type:Int = 3
    static var previews: some View {
        ListViewTags(tag: $tag,type: $type)
    }
}
